package com.bencordingley.remotecontrol;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class TabMain extends TabActivity {

	private static final int MENU_PREFERENCES = 0;
	private static final int MENU_QUIT = 1;
	
	private BroadcastReceiver phoneStateReceive;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabhost);
		
		phoneStateReceive = new PhoneStateReceiver();
		registerReceiver(phoneStateReceive, new IntentFilter("android.intent.action.PHONE_STATE"));

		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent(Constants.ACTION_CONTROLS);
		spec = tabHost.newTabSpec("controls").setContent(intent).setIndicator("Controls", res.getDrawable(R.drawable.controls));
		tabHost.addTab(spec);

		intent = new Intent(Constants.ACTION_MEDIA_BROWSER);
		spec = tabHost.newTabSpec("browser").setContent(intent).setIndicator("Media Browser", res.getDrawable(R.drawable.media_browser));
		tabHost.addTab(spec);

		intent = new Intent(Constants.ACTION_PLAYLISTS);
		spec = tabHost.newTabSpec("playlists").setContent(intent).setIndicator("Playlists", res.getDrawable(R.drawable.playlists));
		tabHost.addTab(spec);
		
		startActivity(new Intent(this, Updater.class));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_PREFERENCES, 0, "Preferences").setIcon(android.R.drawable.ic_menu_preferences);
		menu.add(0, MENU_QUIT, 0, "Quit").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(phoneStateReceive);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_QUIT:
			finish();
			return true;
		case MENU_PREFERENCES:
			startActivity(new Intent(Constants.ACTION_SETTINGS));
			return true;
		}
		return false;
	}
}
